﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using static TestFireBroadcastingHostPhone.DataBase.SQL_DataBase;

namespace TestFireBroadcastingHostPhone.DataBase
{
    public class SQL_DataBase : IDisposable
    {
        /// <summary>
        /// SQL服务器名称
        /// </summary>
        public const string SQL_Server_Name = "192.168.1.90";
        /// <summary>
        /// SQL数据库名称
        /// </summary>
        public const string SQL_Data_Base_Name = "盈帜广播功放测试";
        /// <summary>
        /// SQL用户名
        /// </summary>
        public const string SQL_User_Name = "test1";
        /// <summary>
        /// SQL密码
        /// </summary>
        public const string SQL_User_Password = "123456";
        /// <summary>
        /// 广播主机功放性能测试数据表
        /// </summary>
        public const string SQL_Performance_Testing = ".dbo.广播主机功放性能测试数据表";
        /// <summary>
        /// 广播主机功放显示通讯测试数据表
        /// </summary>
        public const string SQL_CommunicateDisplay_Testing = ".dbo.广播主机功放显示通讯测试数据表";
        /// <summary>
        /// 广播主机按键测试数据表
        /// </summary>
        public const string SQL_Key_Testing = ".dbo.广播主机按键测试数据表";
        /// <summary>
        /// 广播主机复测数据表
        /// </summary>
        public const string SQL_ReTest = ".dbo.广播主机功放复测数据表";

        /// <summary>
        /// 数据库中插入产品相关信息
        /// </summary>
        /// <param name="form">数据表格</param>
        /// <param name="product_code">产品条码</param>
        /// <param name="product_id">产品ID</param>
        /// <param name="product_type">产品型号</param>
        /// <param name="product_power">产品功率</param>
        /// <param name="error_inf">错误信息</param>
        public void SQL_InsertProductInfo(string form, string product_code, string product_id, string product_type, string product_power, out string error_inf)
        {
            error_inf = string.Empty;
            string sql_server_name = SQL_Server_Name;
            string sql_data_base_name = SQL_Data_Base_Name;
            string sql_user_name = SQL_User_Name;
            string sql_user_password = SQL_User_Password;
            string table_name = sql_data_base_name + form;// 表格名称
            string sql_connect_command_text = "DATA SOURCE=" + sql_server_name + ";INITIAL CATALOG=" + sql_data_base_name + ";USER ID=" + sql_user_name + ";PWD=" + sql_user_password;// SQL连接类的命令字符串
            string sql_command_text = "INSERT INTO " + table_name + "(" + "产品条码," + "产品ID," + "产品型号," + "产品功率" + ") VALUES (" + "@产品条码," + "@产品ID," + "@产品型号," + "@产品功率" + ")";// SQL命令字符串                                                                                                                                                             // 查询数据库中是否存在相同的product_code
            string check_sql_command_text = "SELECT COUNT(*) FROM " + table_name + " WHERE 产品条码 = @产品条码";
            try
            {
                using (SqlConnection sql_connection = new SqlConnection(sql_connect_command_text))
                {
                    sql_connection.Open();
                    // 创建查询是否存在相同product_code的命令
                    using (SqlCommand check_sql_command = sql_connection.CreateCommand())
                    {
                        check_sql_command.CommandText = check_sql_command_text;
                        check_sql_command.Parameters.AddWithValue("@产品条码", product_code);
                        int exists_count = Convert.ToInt32(check_sql_command.ExecuteScalar());
                        if (exists_count > 0) return;
                        // 如果不存在，则执行插入操作
                        using (SqlCommand sql_command = sql_connection.CreateCommand())
                        {
                            sql_command.CommandText = sql_command_text;
                            sql_command.Parameters.Clear();
                            sql_command.Parameters.AddWithValue("@产品条码", product_code);
                            sql_command.Parameters.AddWithValue("@产品ID", string.IsNullOrEmpty(product_id) ? DBNull.Value : product_id);
                            sql_command.Parameters.AddWithValue("@产品型号", string.IsNullOrEmpty(product_type) ? DBNull.Value : product_type);
                            sql_command.Parameters.AddWithValue("@产品功率", string.IsNullOrEmpty(product_power) ? DBNull.Value : product_power);
                            int count = sql_command.ExecuteNonQuery();
                            if (count != 1)
                            {
                                error_inf = "SQL插入一行数据失败";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error_inf = ex.Message;
            }
        }

        #region -- 性能测试数据表中调用方法
        /// <summary>
        /// 性能测试数据表参数结构体
        /// </summary>
        public struct PerformanceTestResult
        {
            public decimal OutputVoltage { get; set; }
            public string ResultShortFullLoad { get; set; }
            public string ResultBeyondVoltage { get; set; }
            public string PowerExchange { get; set; }
            public string ResultCurrent { get; set; }
            public decimal MinVoltage { get; set; }
            public decimal MaxVoltage { get; set; }
            public decimal Voltage1000Hz { get; set; }
            public decimal Voltage800Hz { get; set; }
            public decimal Voltage900Hz { get; set; }
            public decimal Voltage2000Hz { get; set; }
            public decimal Voltage3000Hz { get; set; }
            public string Result24Voltage { get; set; }
            /// <summary>
            /// 总体测试结果
            /// </summary>
            public string ResultAll { get; set; }
        }
        /// <summary>
        /// 广播主机功放性能测试数据表根据产品条码更新测试数据
        /// </summary>
        /// <param name="product_code">产品条码</param>
        /// <param name="err_info">错误信息</param>
        public void SQL_Rerformance_UpdateProductInfo(string product_code, PerformanceTestResult testResult, out string err_info)
        {
            err_info = string.Empty;
            string sql_server_name = SQL_Server_Name;         //SQL服务器名称
            string sql_data_base_name = SQL_Data_Base_Name;   //SQL数据库名称
            string sql_user_name = SQL_User_Name;             //SQL用户名
            string sql_user_password = SQL_User_Password;     //SQL密码
            string table_name = sql_data_base_name + SQL_Performance_Testing;
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sql_connect_command_text = "DATA SOURCE=" + sql_server_name + ";INITIAL CATALOG=" + sql_data_base_name + ";USER ID=" + sql_user_name + ";PWD=" + sql_user_password;
            #region SQL命令
            string sql_command_text = "UPDATE " + table_name + " SET " +
            "产品条码 = @产品条码," + "功放输出电压 = @功放输出电压," + "满载短路测试结果 = @满载短路测试结果," + "过压恢复测试结果 = @过压恢复测试结果,"
            + "主备电切换测试结果 = @主备电切换测试结果," + "过流恢复测试结果 = @过流恢复测试结果," + "最大过流输出电压 = @最大过流输出电压," + "最小过流输出电压 = @最小过流输出电压,"
            + "音频1000Hz有效值 = @音频1000Hz有效值," + "音频800Hz有效值 = @音频800Hz有效值," + "音频900Hz有效值 = @音频900Hz有效值," 
            + "音频2000Hz有效值 = @音频2000Hz有效值," + "音频3000Hz有效值 = @音频3000Hz有效值," + "应急启动24V测试结果 = @应急启动24V测试结果,"
            + "测试结果 = @测试结果," + "测试时间 = @测试时间 " + "WHERE 产品条码 = '" + product_code + "'";
            #endregion
            try
            {
                using (SqlConnection sql_connection = new SqlConnection(sql_connect_command_text))
                using (SqlCommand sql_command = sql_connection.CreateCommand())
                {
                    if (sql_connection.State != ConnectionState.Open) sql_connection.Open();   //打开数据库
                    sql_command.CommandText = sql_command_text;    //sql命令赋值
                    sql_command.Parameters.Clear();                //清除全部参数
                    if (product_code == string.Empty) sql_command.Parameters.AddWithValue("@产品条码", DBNull.Value);
                    else sql_command.Parameters.AddWithValue("@产品条码", product_code);
                    AddParameterIfNotZero(sql_command, "@功放输出电压", testResult.OutputVoltage);
                    AddParameterIfNotNull(sql_command, "@满载短路测试结果", testResult.ResultShortFullLoad);
                    AddParameterIfNotNull(sql_command, "@过压恢复测试结果", testResult.ResultBeyondVoltage);
                    AddParameterIfNotNull(sql_command, "@主备电切换测试结果", testResult.PowerExchange);
                    AddParameterIfNotNull(sql_command, "@过流恢复测试结果", testResult.ResultCurrent);
                    AddParameterIfNotZero(sql_command, "@最小过流输出电压", testResult.MinVoltage);
                    AddParameterIfNotZero(sql_command, "@最大过流输出电压", testResult.MaxVoltage);
                    AddParameterIfNotZero(sql_command, "@音频1000Hz有效值", testResult.Voltage1000Hz);
                    AddParameterIfNotZero(sql_command, "@音频800Hz有效值", testResult.Voltage800Hz);
                    AddParameterIfNotZero(sql_command, "@音频900Hz有效值", testResult.Voltage900Hz);
                    AddParameterIfNotZero(sql_command, "@音频2000Hz有效值", testResult.Voltage2000Hz);
                    AddParameterIfNotZero(sql_command, "@音频3000Hz有效值", testResult.Voltage3000Hz);
                    AddParameterIfNotNull(sql_command, "@应急启动24V测试结果", testResult.Result24Voltage);
                    AddParameterIfNotNull(sql_command, "@测试结果", testResult.ResultAll);
                    sql_command.Parameters.AddWithValue("@测试时间", time);
                    int count = sql_command.ExecuteNonQuery();
                    if (count != 1)
                    {
                        err_info = "SQL更新一行临时数据失败";
                        sql_command.Dispose();     //释放数据库命令资源
                        sql_connection.Close();    //关闭数据库连接
                        sql_connection.Dispose();  //释放数据库连接资源
                        return;
                    }
                    sql_command.Dispose();     //释放数据库命令资源
                    sql_connection.Close();    //关闭数据库连接
                    sql_connection.Dispose();  //释放数据库连接资源
                }
            }
            catch (Exception ex)
            {
                err_info = ex.ToString();
            }
        }
        
        #endregion

        #region -- 显示通讯数据表中调用方法
        /// <summary>
        /// 显示通讯测试数据结构体
        /// </summary>
        public struct CommunicateTestResult
        {
            public string result_checkself { get; set; }
            public string result_1register { get; set; }
            public string result_2register { get; set; }
            public string result_shieldAmplifier { get; set; }
            public string result_key { get; set; }
            public string result_shieldAreaContorl { get; set; }
            public string result_redio { get; set; }
            public string result_Aux { get; set; }
            public string result_update { get; set; }
            public string result_Memgency { get; set; }
        }
        /// <summary>
        /// 广播主机功放通讯显示测试数据表根据产品条码更新测试数据
        /// </summary>
        /// <param name="product_code">产品条码</param>
        /// <param name="communicateTestResult">通讯显示测试数据</param>
        /// <param name="err_info">错误信息</param>
        public void SQL_CommunicateDisplay_UpdateProductInfo(string product_code,CommunicateTestResult communicateTestResult,out string err_info)
        {
            err_info = string.Empty;
            string sql_server_name = SQL_Server_Name;         //SQL服务器名称
            string sql_data_base_name = SQL_Data_Base_Name;   //SQL数据库名称
            string sql_user_name = SQL_User_Name;             //SQL用户名
            string sql_user_password = SQL_User_Password;     //SQL密码
            string table_name = sql_data_base_name + SQL_CommunicateDisplay_Testing;
            string sql_connect_command_text = "DATA SOURCE=" + sql_server_name + ";INITIAL CATALOG=" + sql_data_base_name + ";USER ID=" + sql_user_name + ";PWD=" + sql_user_password;
            #region SQL命令
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sql_command_text = "UPDATE " + table_name + " SET " +
            "产品条码 = @产品条码,"+ "自检测试结果 = @自检测试结果," + "通道1匹配功放结果 = @通道1匹配功放结果," + "通道2匹配功放结果 = @通道2匹配功放结果," 
            + "屏蔽功放测试结果 = @屏蔽功放测试结果," + "按键测试结果 = @按键测试结果," + "屏蔽区域控制测试结果 = @屏蔽区域控制测试结果," 
            + "录音测试结果 = @录音测试结果," + "AUX切换测试结果 = @AUX切换测试结果," + "更新音源字库 = @更新音源字库," + "手动应急 = @手动应急," 
            + "测试时间 = @测试时间 " + "WHERE 产品条码 = '" + product_code + "'";
            #endregion
            try
            {
                using (SqlConnection sql_connection = new SqlConnection(sql_connect_command_text))
                using (SqlCommand sql_command = sql_connection.CreateCommand())
                {
                    if (sql_connection.State != ConnectionState.Open) sql_connection.Open();   //打开数据库
                    sql_command.CommandText = sql_command_text;    //sql命令赋值
                    sql_command.Parameters.Clear();                //清除全部参数
                    if (product_code == string.Empty) sql_command.Parameters.AddWithValue("@产品条码", DBNull.Value);
                    else sql_command.Parameters.AddWithValue("@产品条码", product_code);
                    AddParameterIfNotNull(sql_command, "@自检测试结果", communicateTestResult.result_checkself);
                    AddParameterIfNotNull(sql_command, "@通道1匹配功放结果", communicateTestResult.result_1register);
                    AddParameterIfNotNull(sql_command, "@通道2匹配功放结果", communicateTestResult.result_2register);
                    AddParameterIfNotNull(sql_command, "@屏蔽功放测试结果", communicateTestResult.result_shieldAmplifier);
                    AddParameterIfNotNull(sql_command, "@按键测试结果", communicateTestResult.result_key);
                    AddParameterIfNotNull(sql_command, "@屏蔽区域控制测试结果", communicateTestResult.result_shieldAreaContorl);
                    AddParameterIfNotNull(sql_command, "@录音测试结果", communicateTestResult.result_redio);
                    AddParameterIfNotNull(sql_command, "@AUX切换测试结果", communicateTestResult.result_Aux);
                    AddParameterIfNotNull(sql_command, "@更新音源字库", communicateTestResult.result_update);
                    AddParameterIfNotNull(sql_command, "@手动应急", communicateTestResult.result_Memgency);
                    sql_command.Parameters.AddWithValue("@测试时间", time);
                    int count = sql_command.ExecuteNonQuery();
                    if (count != 1)
                    {
                        err_info = "SQL更新一行临时数据失败";
                        sql_command.Dispose();     //释放数据库命令资源
                        sql_connection.Close();    //关闭数据库连接
                        sql_connection.Dispose();  //释放数据库连接资源
                        return;
                    }
                    sql_command.Dispose();     //释放数据库命令资源
                    sql_connection.Close();    //关闭数据库连接
                    sql_connection.Dispose();  //释放数据库连接资源
                }
            }
            catch (Exception ex)
            {
                err_info = ex.ToString();
            }
        }
        #endregion

        #region -- 按键测试数据表中调用方法
        /// <summary>
        /// 数据库按键键值类
        /// </summary>
        public class KeyNumber
        {
            public string? KeyBroadcast { set; get; }// 通播键
            public string? KeyNumber1 { set; get; }// 按键1
            public string? KeyNumber2 { set; get; }// 按键2
            public string? KeyNumber3 { set; get; }// 按键3
            public string? KeyBack { set; get; }// 返回键
            public string? KeyEmergency { set; get; }//应急键
            public string? KeyNumber4 { set; get; }// 按键4
            public string? KeyNumber5 { set; get; }// 按键5
            public string? KeyNumber6 { set; get; }// 按键6
            public string? KeyNumber0 { set; get; }// 按键0
            public string? KeySlience { set; get; }// 消音键
            public string? KeyNumber7 { set; get; }// 按键7
            public string? KeyNumber8 { set; get; }// 按键8
            public string? KeyNumber9 { set; get; }// 按键9
            public string? KeyEnter { set; get; }// 确认键
            public string? ProductCode { set; get; }//产品条码
        }

        /// <summary>
        /// 获取数据库中表头的字典
        /// </summary>
        public Dictionary<string, string> propertyChineseNames = new Dictionary<string, string>{
            { nameof(KeyNumber.ProductCode), "产品条码" },
            { nameof(KeyNumber.KeyBroadcast), "通播键" },
            { nameof(KeyNumber.KeyNumber1), "按键1" },
            { nameof(KeyNumber.KeyNumber2), "按键2" },
            { nameof(KeyNumber.KeyNumber3), "按键3" },
            { nameof(KeyNumber.KeyBack), "返回键" },
            { nameof(KeyNumber.KeyEmergency), "应急键" },
            { nameof(KeyNumber.KeyNumber4), "按键4" },
            { nameof(KeyNumber.KeyNumber5), "按键5" },
            { nameof(KeyNumber.KeyNumber6), "按键6" },
            { nameof(KeyNumber.KeyNumber0), "按键0" },
            { nameof(KeyNumber.KeySlience), "消音键" },
            { nameof(KeyNumber.KeyNumber7), "按键7" },
            { nameof(KeyNumber.KeyNumber8), "按键8" },
            { nameof(KeyNumber.KeyNumber9), "按键9" },
            { nameof(KeyNumber.KeyEnter), "确认键" },
        };
        /// <summary>
        /// 广播主机功放按键测试数据表更新测试数据
        /// </summary>
        /// <param name="zeroPointCalibrationRecord">按键键值类</param>
        /// <param name="error_inf"></param>
        public void SQL_Key_UpdateProductInfo(ref KeyNumber keynumber, out string err_info)
        {
            err_info = string.Empty;
            string sql_server_name = SQL_Server_Name;         //SQL服务器名称
            string sql_data_base_name = SQL_Data_Base_Name;   //SQL数据库名称
            string sql_user_name = SQL_User_Name;             //SQL用户名
            string sql_user_password = SQL_User_Password;     //SQL密码
            string table_name = sql_data_base_name + SQL_Key_Testing;
            string sql_connect_command_text = $"DATA SOURCE={sql_server_name};INITIAL CATALOG={sql_data_base_name};USER ID={sql_user_name};PWD={sql_user_password}";
            string sql_command_text = $"UPDATE {table_name} SET ";
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            // 获取结构体的所有属性
            PropertyInfo[] properties = typeof(KeyNumber).GetProperties();
            List<string> setClauses = new List<string>();
            // 构造SET子句
            foreach (var prop in properties)
            {
                if (propertyChineseNames.ContainsKey(prop.Name))
                {
                    // 使用属性的中文名称替换英文名称
                    string chineseName = propertyChineseNames[prop.Name]; // 移除空格以适合作为参数名
                    setClauses.Add($"{chineseName} = @{chineseName}");
                }
            }
            sql_command_text += string.Join(",", setClauses) + ",测试时间 = @测试时间 ";
            sql_command_text += " WHERE 产品条码 = @产品条码";            // 添加WHERE子句
            try
            {
                using (SqlConnection sql_connection = new SqlConnection(sql_connect_command_text))
                {
                    sql_connection.Open();
                    using (SqlCommand sql_command = sql_connection.CreateCommand())
                    {
                        sql_command.CommandText = sql_command_text;
                        sql_command.Parameters.Clear();
                        foreach (var prop in properties)
                        {
                            if (propertyChineseNames.TryGetValue(prop.Name, out string? chinesePropertyName))
                            {
                                // 避免对已在循环外处理的特殊属性再次处理
                                if (chinesePropertyName != "产品条码")
                                {
                                    var value = prop.GetValue(keynumber);
                                    string paramName = $"@{chinesePropertyName}";
                                    sql_command.Parameters.AddWithValue(paramName, value ?? DBNull.Value);
                                }
                            }
                        }
                        if (keynumber.ProductCode == string.Empty) sql_command.Parameters.AddWithValue("@产品条码", DBNull.Value);   //添加参数，ID，无效数据
                        else sql_command.Parameters.AddWithValue("@产品条码", keynumber.ProductCode);                      //添加参数，ID，有效数据
                        sql_command.Parameters.AddWithValue("@测试时间", time);
                        int count = 0;
                        //MessageBox.Show(sql_command.CommandText);
                        count = sql_command.ExecuteNonQuery();         //执行sql命令

                        if (count != 1)
                        {
                            err_info = "SQL更新一行临时数据失败";
                            sql_command.Dispose();     //释放数据库命令资源
                            sql_connection.Close();    //关闭数据库连接
                            sql_connection.Dispose();  //释放数据库连接资源
                        }
                        sql_command.Dispose();     //释放数据库命令资源
                        sql_connection.Close();    //关闭数据库连接
                        sql_connection.Dispose();  //释放数据库连接资源
                    }
                }
            }
            catch (Exception ex)
            {
                err_info = "SQL更新一行临时数据失败: " + ex.Message;
            }
        }
        #endregion

        #region -- 复测数据表调用方法
        /// <summary>
        /// 复测数据表结构体
        /// </summary>
        public struct ReTestResult
        {
            public string result_powerchange { get; set; }
            public string result_current { set; get; }
            public decimal min_voltage { set; get; }
            public decimal max_voltage { set; get; }
            public string result_voltage { set; get; }
            public string result_1register { set; get; }
            public string result_2register { set; get; }
            public string result_shield { set; get; }
            /// <summary>
            /// 整体测试结果
            /// </summary>
            public string result_all {  set; get; }
        }
        /// <summary>
        /// 广播主机功放复测数据表根据产品条码更新测试数据
        /// </summary>
        /// <param name="product_code">产品条码</param>
        /// <param name="reTestResult">复测结果数据</param>
        /// <param name="err_info">错误信息</param>
        public void SQL_ReTest_UpdateProductInfo(string product_code, ReTestResult reTestResult, out string err_info)
        {
            err_info = string.Empty;
            err_info = string.Empty;
            string sql_server_name = SQL_Server_Name;         //SQL服务器名称
            string sql_data_base_name = SQL_Data_Base_Name;   //SQL数据库名称
            string sql_user_name = SQL_User_Name;             //SQL用户名
            string sql_user_password = SQL_User_Password;     //SQL密码
            string table_name = sql_data_base_name + SQL_ReTest;//复测数据表
            string sql_connect_command_text = "DATA SOURCE=" + sql_server_name + ";INITIAL CATALOG=" + sql_data_base_name + ";USER ID=" + sql_user_name + ";PWD=" + sql_user_password;
            #region SQL命令
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string sql_command_text = "UPDATE " + table_name + " SET " +
            "产品条码 = @产品条码," + "主备电切换功能 = @主备电切换功能," + "过流恢复功能 = @过流恢复功能," + "最小过流电压 = @最小过流电压,"
            + "最大过流电压 = @最大过流电压," + "过压恢复功能 = @过压恢复功能," + "通道1匹配功放功能 = @通道1匹配功放功能,"
            + "通道2匹配功放功能 = @通道2匹配功放功能," + "屏蔽功放功能 = @屏蔽功放功能,"
            + "测试结果 = @测试结果," + "测试时间 = @测试时间 " + "WHERE 产品条码 = '" + product_code + "'";
            #endregion
            try
            {
                using (SqlConnection sql_connection = new SqlConnection(sql_connect_command_text))
                using (SqlCommand sql_command = sql_connection.CreateCommand())
                {
                    if (sql_connection.State != ConnectionState.Open) sql_connection.Open();   //打开数据库
                    sql_command.CommandText = sql_command_text;    //sql命令赋值
                    sql_command.Parameters.Clear();                //清除全部参数
                    if (product_code == string.Empty) sql_command.Parameters.AddWithValue("@产品条码", DBNull.Value);
                    else sql_command.Parameters.AddWithValue("@产品条码", product_code);
                    AddParameterIfNotNull(sql_command, "@主备电切换功能", reTestResult.result_powerchange);
                    AddParameterIfNotNull(sql_command, "@过流恢复功能", reTestResult.result_current);
                    AddParameterIfNotZero(sql_command, "@最小过流电压", reTestResult.min_voltage);
                    AddParameterIfNotZero(sql_command, "@最大过流电压", reTestResult.max_voltage);
                    AddParameterIfNotNull(sql_command, "@过压恢复功能", reTestResult.result_voltage);
                    AddParameterIfNotNull(sql_command, "@通道1匹配功放功能", reTestResult.result_1register);
                    AddParameterIfNotNull(sql_command, "@通道2匹配功放功能", reTestResult.result_2register);
                    AddParameterIfNotNull(sql_command, "@屏蔽功放功能", reTestResult.result_shield);
                    AddParameterIfNotNull(sql_command, "@测试结果", reTestResult.result_all);
                    sql_command.Parameters.AddWithValue("@测试时间", time);
                    int count = sql_command.ExecuteNonQuery();
                    if (count != 1)
                    {
                        err_info = "SQL更新一行临时数据失败";
                        sql_command.Dispose();     //释放数据库命令资源
                        sql_connection.Close();    //关闭数据库连接
                        sql_connection.Dispose();  //释放数据库连接资源
                        return;
                    }
                    sql_command.Dispose();     //释放数据库命令资源
                    sql_connection.Close();    //关闭数据库连接
                    sql_connection.Dispose();  //释放数据库连接资源
                }
            }
            catch (Exception ex)
            {
                err_info = ex.ToString();
            }
        }
        #endregion

        /// <summary>
        /// 辅助方法：数据库为空值判断
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        private static void AddParameterIfNotNull(SqlCommand command, string parameterName, string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                command.Parameters.AddWithValue(parameterName, value);
            }
            else
            {
                command.Parameters.AddWithValue(parameterName, DBNull.Value);
            }
        }
        /// <summary>
        /// 辅助方法：数据库为0判断
        /// </summary>
        /// <param name="command"></param>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        private static void AddParameterIfNotZero(SqlCommand command, string parameterName, decimal value)
        {
            if (value != 0)
            {
                command.Parameters.AddWithValue(parameterName, value);
            }
            else
            {
                command.Parameters.AddWithValue(parameterName, DBNull.Value);
            }
        }
        #region --垃圾回收机制
        //变量定义
        /// <summary>
        /// 是否已释放资源的标志
        /// </summary>
        private bool m_disposed = false;

        /// <summary>
        /// 释放内存中所占的资源
        /// </summary>
        public void Dispose()
        {
            Dispose(true);   //释放托管和非托管资源

            //将对象从垃圾回收器链表中移除，
            // 从而在垃圾回收器工作时，只释放托管资源，而不执行此对象的析构函数
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 无法直接调用的资源释放函数
        /// </summary>
        /// <param name="disposing"></param>
        // 参数为true表示释放所有资源，只能由使用者调用
        // 参数为false表示释放非托管资源，只能由垃圾回收器自动调用
        // 如果子类有自己的非托管资源，可以重载这个函数，添加自己的非托管资源的释放
        // 但是要记住，重载此函数必须保证调用基类的版本，以保证基类的资源正常释放
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    //释放托管资源   
                }
                //释放非托管资源        
                m_disposed = true;   //标志此对象已被释放
            }
        }

        /// <summary>
        /// 析构函数
        /// 由垃圾回收器调用，释放非托管资源
        /// </summary>
        ~SQL_DataBase()
        {
            Dispose(false);
        }

        #endregion
    }
}
